// 初始化权限列表



// 获取当前时间的前一个月/秒时间，解决时间选择器出现1970的bug
function getPreMonth () {
  var prve = new Date(new Date().getTime()+1000)
  var formatwdate = new Date(prve)
  return formatwdate
}

const common = {
  state: {
    nullData: '-',
    publicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRXYFYTGI9uVipfl9P5loLAWLRIQPpSznBc1ACIpCO/ptKYLXjzunWz2TyCj5OV1yjs9pEIcyOnxs6ESplsUOsEakf6wDgox6sU3A51mQmQlm6ALxtfguurZGOJ0Ksg/gL1q97YWTSMsH9R1slDV95nvMKsQAd4Yd/6i+2/ihaxQIDAQAB',
    lang: global.config.lang, // 切换语言版本 vi 越南  id 印尼 PHL 菲律宾
    id_currency: global.config.first_unit, // 印尼金钱单位 Rp
    vi_currency: global.config.last_unit, //越南金钱单位 VND 菲律宾金钱单位PHP
    permiss: sessionStorage.rolelist?JSON.parse(sessionStorage.rolelist):"",
    preMonth: getPreMonth(),
    topMenu:sessionStorage.topMenu,
  },
  getters: {
    moneySplit: () => (str) => { // 金钱做处理，印尼、越南'$1.' 菲律宾'$1,'
      if(!str){
        return '-';
      }else{
        return String(str).split('.').map(
          function (val, index) {
            return index == 1 ? val : val.split('').reverse().join('').replace(/(\d{3})\B/g, global.config.moneySplit).split('').reverse().join('')
          }
        ).join('.')
      }    
    },
    yyyy_m_d: () => (str) => {
      var date = new Date(str)
      let Y = date.getFullYear() + '-'
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
      return Y + M + D
    },
    twoPoint: () => (str) => { // 截取小数点后两位
      if(typeof str === 'undefined' || str === '' || !str){
        return '-'
      }else{
        // var aNew
        // var re = /([0-9]+\.[0-9]{2})[0-9]*/
        // aNew = String(str * 100).replace(re, '$1')
        // return aNew+'%'
        var value = Math.round(parseFloat(str) * 100);
        var xsd = value.toString().split(".");
        if (xsd.length == 1) {
            value = value.toString() + ".00%";
            return value;
        }
        if (xsd.length > 1) {
            if (xsd[1].length < 2) {
                value = value.toString() + "0%";
            }
            return value;
        }
      }
      
    },
    gettoFixed: () => (str) => { // 截取小数点后两位
      if(typeof str === 'undefined' || str === '' || !str){
        return ''
      }else{
        // var aNew
        // var re = /([0-9]+\.[0-9]{2})[0-9]*/
        // aNew = String(str * 100).replace(re, '$1')
        // return aNew+'%'
        var value = parseFloat(str);
        var xsd = value.toString().split(".");
        if (xsd.length == 1) {
            value = value.toString() + ".00";
            return value;
        }
        if (xsd.length > 1) {
            if (xsd[1].length < 2) {
                value = value.toString() + "0";
            }
            return value;
        }
      }
      
    },
    uniqueArray: () => (arr) => { // 数组去重
      var res = []
      var json = {}
      for (var i = 0; i < arr.length; i++) {
        if (!json[arr[i]]) {
          res.push(arr[i])
          json[arr[i]] = 1
        }
      }
      return res
    },
    isToady: () => (str) => { // 判断是否是今日
      if (String(new Date(str)).slice(0, 15) === String(new Date()).slice(0, 15)) {
        return true
      } else {
        return false
      }
    },
    getYMD: () => (str) => {  //时间戳转换为时间格式
      var date = new Date(Number(str))
      let Y = date.getFullYear() + '-'
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      let D = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()
      return Y + M + D

    },
    getYMDHMS: () => (str) => {
      var date = new Date(Number(str))
      let Y = date.getFullYear() + '-'
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      let D = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()
      let H = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
      let Mi = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
      let S = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()
      return `${Y}${M}${D} ${H}:${Mi}:${S}`
    },
    getAge: () => (str) => { // 根据时间获取年龄
      if(!str){
        return '-'
      }else{
        var strBirthdayArr = String(str).split('-')
        var birthYear = strBirthdayArr[0]

        var d = new Date()
        var nowYear = d.getFullYear()

        var age = nowYear - birthYear
        return age
      }
      
    },
    longitudeAndLatitude: ()=>(str)=>{// 转换经纬度
      if(str==0){
        return '-'
      }else{
        var num = String(str);
        var du = num.split('.')[0];
        var str1 = num.split('.')[1];
        var str2 = Number('0.'+str1)*60;
        var str4 = String(str2);
        var fen = str4.split('.')[0];
        var str3 = str4.split('.')[1];
        var miao = Number(Number('0.'+str3)*60).toFixed(2);
        return `${du}°${fen}′${miao}″`
      }
    }
  },
  mutations: {
    setTopMenu(state,key){
      state.topMenu=key
    },
    setPermiss(state,list){
      state.permiss=JSON.parse(list)
    },
    setLang(state,data){
      state.lang=data.lang;
      state.id_currency=data.first_unit;
      state.vi_currency=data.last_unit;
    },
    filterPreList (state, list) { // 筛选初符合条件的路由
      let arr = []
      JSON.parse(list.list).forEach(item => {
        if (item&&item.uniqueSign) {
          arr.push(item.uniqueSign)
        }
      })
      state.permiss = arr
    },
    getPreMouth (state) {
      var nowdate = new Date()
      nowdate.setMonth(nowdate.getMonth() - 1)
      var y = nowdate.getFullYear()
      var m = nowdate.getMonth() + 1
      var d = nowdate.getDate()
      var formatwdate = y + '-' + m + '-' + d
      state.PreMouth = formatwdate
    }
  }

}

export default common
